package com.emm.yixun.estate.service.impl.app.manage;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.ProjectUnitService;
import com.emm.yixun.common.app.GetUnitListReqDto;
import com.emm.yixun.common.app.GetUnitListResDto;
import com.emm.yixun.common.app.servletentity.UnitListRes;
import com.emm.yixun.common.enums.ResultEnum;
import com.emm.yixun.common.model.ProjectUnit;
import com.emm.yixun.common.model.dto.ProjectUnitDto;

@Service("getUnitListManage")
public class GetUnitListManage {

	@Autowired
	private ProjectUnitService projectUnitService;
	/**
	 * 查询可关注房源-单元
	 * @param reqDto
	 * @return
	 */
	public GetUnitListResDto getUnitList(GetUnitListReqDto reqDto) {
		GetUnitListResDto resDto = new GetUnitListResDto();
		try{
			ProjectUnitDto puDto = new ProjectUnitDto();
			puDto.setMerchantId(reqDto.getMerchantId());
			puDto.setProjectCode(reqDto.getProjectCode());
			if(reqDto.getBuildingId() != null && !"".equals(reqDto.getBuildingId())){
				puDto.setBuildingId(Long.parseLong(reqDto.getBuildingId()));
			}
			if(reqDto.getPremisesId() != null && !"".equals(reqDto.getPremisesId())){
				puDto.setPremisesId(Long.parseLong(reqDto.getPremisesId()));
			}
			List<ProjectUnit> puList = projectUnitService.selectListByDto(puDto);
			List<UnitListRes> unitList = new ArrayList<UnitListRes>();
			if(puList.size() > 0){
				for(ProjectUnit pu: puList){
					UnitListRes ulRes = new UnitListRes();
					ulRes.setUnitId(String.valueOf(pu.getId()));
					ulRes.setUnitName(pu.getUnitName());
					unitList.add(ulRes);
				}
				resDto.setUnitList(unitList);
			}else{
				resDto.setUnitList(null);
			}
			resDto.setResult(ResultEnum.SUCCESS.getNo());
		}catch(Exception e){
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("可关注单元查询失败!");
			resDto.setResult(ResultEnum.FAILURE.getNo());
		}
		return resDto;
	}
	
}
